<?php
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/../../library'),
    get_include_path(),
)));

define('SCRIPT_PATH', APPLICATION_PATH . '/../scripts');

require_once 'Zend/Loader/Autoloader.php';
require_once APPLICATION_PATH . '/include/constants.php';
Zend_Loader_Autoloader::getInstance();

session_save_path(APPLICATION_PATH . '/../public/cache');
$_SERVER['HTTPS'] = false;
$_SERVER['SERVER_NAME'] = 'localhost';


$application = new Zend_Application(
    APPLICATION_ENV,
    array(
    	'config' => APPLICATION_PATH . '/configs/application.ini',
    	'autoloadernamespaces' => array('MKT_')
    ));

$application->bootstrap();
?>

<?php
function countThread($command)
{
    $output = array();
    exec("ps aux | grep $command", $output);
    return sizeof($output) - 2;
}
?>

<?php 
//function startDaemonUpdate($action_type) {
//    $db = NULL;
//    $update_script_name = "";
//    if ($action_type == COMMENT) {
//        $db = new Core_Model_DbTable_BufferComments();
//        $update_script_name = "slave_update_comments_detail.php";
//    } elseif ($action_type == POST) {
//        $db = new Core_Model_DbTable_BufferPosts();
//        $update_script_name = "slave_update_posts_detail.php";
//    } else {
//        $db = new Core_Model_DbTable_BufferLikes();
//        $update_script_name = "slave_update_likes_detail.php"; 
//    }
//    
//    while (true) 
//    {
//        $thread_count = countThread($update_script_name);
//        $new_thread = MAX_THREAD - $thread_count;
//
//        if ($new_thread > 0) {
//            $entry_count = BATCH_ITEM_COUNT * $new_thread;
//
//            $entries = $db->getEntriesForUpdating($entry_count);
//            if ($entries->count() > 0)
//            {
//                $array_entries = $entries->toArray();
//                $offset = 0;
//                while ($batch_entries = array_slice($array_entries, $offset, BATCH_ITEM_COUNT))
//                {
//                    $str_entry_ids = getStrIds($batch_entries);
//                    exec("php " . SCRIPT_PATH . "/$update_script_name \"$str_entry_ids\" > /dev/null &");
//                    $offset += BATCH_ITEM_COUNT;
//                }            
//            }
//            else
//            {
//                sleep(60);
//            }    
//        } else {
//            sleep(60);
//        }
//    }
//}
?>

<?php
function getStrIds($entries)
{
    $entry_ids = array();
       
    foreach ($entries as $entry) 
    {
        $entry_ids[] = $entry['id'];
    }
    return implode(",", $entry_ids);
}
?>

<?php 
function startDaemonUpdate($id_social_page, $end_time, $action_type) {
    $max_thread = 10;
    $db = NULL;
    $update_script_name = "";
    if ($action_type == COMMENT) {
        $db = new Core_Model_DbTable_BufferComments();
        $update_script_name = "slave_update_comments_detail.php";
    } elseif ($action_type == POST) {
        $db = new Core_Model_DbTable_BufferPosts();
        $update_script_name = "slave_update_posts_detail.php";
    } else {
        $db = new Core_Model_DbTable_BufferLikes();
        $update_script_name = "slave_update_likes_detail.php"; 
    }
    
    while (true) {
        $thread_count = countThread($update_script_name);
        $new_thread = $max_thread - $thread_count;
        
        if ($new_thread > 0) {
            $entry_count = BATCH_ITEM_COUNT * $new_thread;
            
            $entries = $db->getEntriesForUpdating($id_social_page, $end_time, $entry_count);
            if ($entries->count() > 0) {
                $array_entries = $entries->toArray();
                $offset = 0;
                while ($batch_entries = array_slice($array_entries, $offset, BATCH_ITEM_COUNT))
                {
                    $str_entry_ids = getStrIds($batch_entries);
                    exec("php " . SCRIPT_PATH . "/$update_script_name \"$str_entry_ids\" > /dev/null &");
                    $offset += BATCH_ITEM_COUNT;
                }            
            }
            else
            {
                if (!$db->hasFinishUpdated($id_social_page, $end_time)) {
                    sleep(30);
                } else {
                    break; //cancel updating
                }
            }    
        } else {
            sleep(10);
        }
    }
    
//    while (true) 
//    {
//        $thread_count = countThread($update_script_name);
//        $new_thread = MAX_THREAD - $thread_count;
//
//        if ($new_thread > 0) {
//            $entry_count = BATCH_ITEM_COUNT * $new_thread;
//
//            $entries = $db->getEntriesForUpdating($entry_count);
//            if ($entries->count() > 0)
//            {
//                $array_entries = $entries->toArray();
//                $offset = 0;
//                while ($batch_entries = array_slice($array_entries, $offset, BATCH_ITEM_COUNT))
//                {
//                    $str_entry_ids = getStrIds($batch_entries);
//                    exec("php " . SCRIPT_PATH . "/$update_script_name \"$str_entry_ids\" > /dev/null &");
//                    $offset += BATCH_ITEM_COUNT;
//                }            
//            }
//            else
//            {
//                sleep(60);
//            }    
//        } else {
//            sleep(60);
//        }
//    }
}
?>